package com.atguigu.gmall.common.executor;

import com.atguigu.gmall.common.executor.properties.ThreadPoolProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author:lyd
 * @Date:2023/8/24 14:21
 * @Version:1.0
 * @Description
 **/
@Slf4j
@EnableConfigurationProperties(ThreadPoolProperties.class)//1、开启绑定 2、把组件放到容器中
@Configuration
public class ThreadPoolConfiguration {

    //这个bean和上面的@EnableConfigurationProperties注解配置是一样的功能
//    @Bean
//    public ThreadPoolProperties properties() {
//        return new ThreadPoolProperties();
//    }

    @Bean
    public ThreadPoolExecutor executor(ThreadPoolProperties properties) {
        log.info("正在给容器中准备自定义线程池");
        return new ThreadPoolExecutor(
                properties.getCore(),
                properties.getMax(),
                properties.getKeepAliveTime(),
                properties.getUnit(),
                new LinkedBlockingQueue<>(properties.getWorkQueueSize()),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }

}
